package com.yhsimon.prototype.deepclone;

public class Client {
    public static void main(String[] args) throws CloneNotSupportedException {
        DeepProtoType p1 = new DeepProtoType();
        p1.name="憨憨";
        p1.deepCloneableTarget=new DeepCloneableTarget("小陈大人","小陈大人类");
        System.out.println(p1.toString());

        //方式1  重写clone方法完成深拷贝
        DeepProtoType p2 = (DeepProtoType) p1.clone();
        System.out.println(p1.hashCode());
        System.out.println(p2.hashCode());
        System.out.println(p1.hashCode()==p2.hashCode());
        System.out.println("克隆前后的引用对象的哈希值是否相同：");
        //结果是false  所以对引用对象的克隆使用硬拷贝
        System.out.println(p1.deepCloneableTarget.hashCode()==p2.deepCloneableTarget.hashCode());
        System.out.println();
        //方式2 通过对象的序列化实现 (推荐)
        System.out.println("--------------方式2------------");
        DeepProtoType p3= (DeepProtoType) p1.deepClone();
        System.out.println(p1.hashCode());
        System.out.println(p3.hashCode());
        System.out.println(p1.hashCode()==p3.hashCode());
        System.out.println("克隆前后的引用对象的哈希值是否相同：");
        //结果是false  所以对引用对象的克隆使用硬拷贝
        System.out.println(p1.deepCloneableTarget.hashCode()==p3.deepCloneableTarget.hashCode());
    }
}
